<template>
  <div class="container-fluid">
    <div class="common_title">
      用户信息修改
    </div>
    <div class="container-fluid common_con">
      <form class="form-horizontal article_form" id="form" ref="form">
        <div class="form-group">
          <label for="inputEmail1" class="col-sm-2 control-label"
            >用户名称：</label
          >
          <div class="col-sm-4">
            <input
              v-model="userInfo.username"
              type="text"
              name="username"
              class="form-control username"
              id="inputEmail1"
              value="李宗盛"
            />
          </div>
        </div>
        <div class="form-group">
          <label for="inputEmail2" class="col-sm-2 control-label">昵称：</label>
          <div class="col-sm-4">
            <input
              v-model="userInfo.nickname"
              type="text"
              name="nickname"
              class="form-control nickname"
              id="inputEmail2"
              value="保健坤"
            />
          </div>
        </div>
        <div class="form-group">
          <label for="inputEmail3" class="col-sm-2 control-label">邮箱：</label>
          <div class="col-sm-4">
            <input
              v-model="userInfo.email"
              type="text"
              name="email"
              class="form-control email"
              id="inputEmail3"
              value="520@163.com"
            />
          </div>
        </div>

        <div class="form-group">
          <label for="inputPassword3" class="col-sm-2 control-label"
            >用户图标：</label
          >
          <div class="col-sm-10">
            <img :src="userInfo.userPic" alt="" class="user_pic" />
            <input
              style="display: none"
              name="userPic"
              type="file"
              id="exampleInputFile"
              @change="doChange"
              ref="file"
            />
            <!-- 给label标签的for属性赋值,值是input标签的id,那这样的话点击label就相当于点击了input -->
            <br />
            <label for="exampleInputFile">
              <img
                src="~@/assets/images/uploads_icon.jpg"
                style="height: 50px;"
                alt=""
              />
            </label>
          </div>
        </div>

        <div class="form-group">
          <label for="inputEmail4" class="col-sm-2 control-label">密码：</label>
          <div class="col-sm-4">
            <input
              v-model="userInfo.password"
              type="password"
              name="password"
              class="form-control password"
              id="inputEmail4"
              value="12345678"
            />
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button
              type="submit"
              class="btn btn-success btn-edit"
              @click.prevent="doEdit"
            >
              修改
            </button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
//导入接口
import { userEditInfo } from './user.js'
export default {
  namme: 'user',
  data () {
    return {
      userInfo: {
        
      }
    }
  },
  created () {
    /* 这里为什么不需要mapState来生成计算属性呢？
    1.页面表单需要双向绑定，而计算属性默认只能获取，不能设置
    2.页面不能直接去修改vuex数据，需要用户点击修改服务器成功才可以
      */
    this.userInfo = {
        ...this.$store.state.userInfo
    }
  },
  methods: {
    /* 选择文件事件 */
    doChange () {
      //(1)获取用户选择的文件
      let file = this.$refs.file.files[0]
      //(2)生成blob路径
      this.userInfo.userPic = URL.createObjectURL(file)
    },
    async doEdit () {
      //(1)创建formdata
      let fd = new FormData(this.$refs.form)
      //(2)发送请求
      let res = await userEditInfo(fd)
      console.log(res)
      //(3)弹窗提示
      this.$alert('修改成功')
      //(3)更新vuex
      this.$store.commit('updateUserInfo',this.userInfo)
    }
  }
}
</script>

<style></style>
